package com.mrdeer.web.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mrdeer.common.JSONResult;
import com.mrdeer.common.constants.CommonConstant;
import com.mrdeer.common.entity.dto.MessageSendDTO;
import com.mrdeer.common.entity.sys.MessageSend;
import com.mrdeer.common.entity.sys.User;
import com.mrdeer.security.SecurityUtil;
import com.mrdeer.web.service.sys.SysMessageSendService;
import com.mrdeer.web.service.sys.SysMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@Api(tags = "消息发送管理接口")
@RequestMapping("/mrdeer/messageSend")
@Transactional
public class SysMessageSendController {

    @Resource
    private SysMessageService sysMessageService;

    @Resource
    private SysMessageSendService sysMessageSendService;

    @Resource
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/getMessageSendByCondition", method = RequestMethod.POST)
    @ApiOperation(value = "多条件分页获取")
    public JSONResult getByCondition(@RequestBody MessageSendDTO dto) {
        try {
            Page<MessageSend> page = sysMessageSendService.list(dto);
            return JSONResult.ok(page);
        } catch (Exception e) {
            return JSONResult.errorMsg(e.getMessage());
        }
    }

    @RequestMapping(value = "/all/{type}", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public JSONResult batchOperation(@Param("0全部已读 1全部删除已读") @PathVariable Integer type) {
        User u = securityUtil.getCurrUser();
        if (type == 0) {
            sysMessageSendService.updateStatusByUserId(u.getId(), CommonConstant.MESSAGE_STATUS_READ);
        } else if (type == 1) {
            sysMessageSendService.updateStatusByUserId(u.getId(), CommonConstant.MESSAGE_STATUS_UNREAD);
        }
        return JSONResult.ok("操作成功");
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑")
    public JSONResult edit(MessageSend messageSend) {
        if (sysMessageService.getById(messageSend.getMessageId()) != null) {
            sysMessageSendService.updateById(messageSend);
        }
        return JSONResult.ok("操作成功");
    }
}
